-- lists the -id- and -title- of all posts. While there is a text attribute, we skip that
local url = 'http://localhost:3000/'

addCommandHandler('posts',
  function(player, command)
    callRemote(url .. 'posts.mta',
      function(posts, err)
        if posts ~= 'ERROR' then
          output(player, #posts .. ' posts:')
          for _, post in ipairs(posts) do
            output(player, ' #' .. post.id .. ': ' .. tostring(post.title))
          end
        else
          output(player, 'Failed to fetch posts')
        end
      end,
      nil,
      { method = 'GET' }
    )
  end
)

addCommandHandler('post',
  function(player, command, id)
    if tonumber(id) then
      callRemote(url .. 'posts/' .. math.floor(tonumber(id)) .. '.mta',
        function(post, err)
          if post ~= 'ERROR' then
            output(player, 'Post #' .. post.id .. ': ' .. tostring(post.title))
            -- first 60 characters only
            output(player, tostring(post.text):sub(1, 60))
          else
            output(player, 'Failed to fetch post')
          end
        end,
        nil,
        { method = 'GET'}
      )
    else
      output(player, 'Missing id, /' .. command .. ' id')
    end
  end
)

addCommandHandler('newpost',
  function(player, command, title, ...)
    callRemote(url .. 'posts.mta',
      function(post, err)
        if post ~= 'ERROR' then
          output(player, 'Created Post #' .. post.id .. ': ' .. tostring(post.title))
          -- first 60 characters only
          output(player, tostring(post.text):sub(1, 60))

          -- rails sets the location like this:
          -- format.json { render json: @post, status: :created, location: @post }
          -- err contains our headers if no error was received.
          output(player, 'URL: ' .. err['Location'])
        else
          output(player, 'Failed to create post')
        end
      end,
      { post = { title = title, text = table.concat({...}, ' ') } }
    )
  end
)

addCommandHandler('updatepost',
  function(player, command, id, title, ...)
    if tonumber(id) then
      callRemote(url .. 'posts/' .. math.floor(tonumber(id)) .. '.mta',
        function(post, err)
          if post ~= 'ERROR' then
            -- the controller generated by the default update has
            -- head :no_content - which means both post and err will be empty.
            output(player, 'Updated!')
          else
            output(player, 'Failed to update post')
          end
        end,
        { post = { title = title, text = table.concat({...}, ' ') } },
        { method = 'PUT' }
      )
    end
  end
)

addCommandHandler('delpost',
  function(player, command, id)
    if tonumber(id) then
      callRemote(url .. 'posts/' .. math.floor(tonumber(id)) .. '.mta',
        function(post, err)
          if post ~= 'ERROR' then
            -- the controller generated by the default update has
            -- head :no_content - which means both post and err will be empty.
            output(player, 'deleted!')
          else
            output(player, 'Failed to delete post')
          end
        end,
        nil,
        { method = 'DELETE' }
      )
    end
  end
)

function output(element, text)
  if getElementType(element) == 'console' then
    outputServerLog(text)
  else
    outputChatBox(text, element)
  end
end
